package pokeronlab_2014.mcts;

import pokeronlab_2014.Controller;
import pokeronlab_2014.SVMClassifierData;
import pokeronlab_2014.mcts.TreeNode.NodeType;

public class BotNode extends PlayerNode {
	
	
	public BotNode() {
		//Controller.debugWindow.printMessage("BotNode.BotNode");
		parent = null;
		SVMbemenet = new SVMClassifierData();
		typeOfNode = NodeType.playeraction_my;
		nVisits = 0;
		totValue = 0;
	}
	
	public TreeNode simulate(TreeNode currentNode) {
		
		//Controller.debugWindow.printMessage("BotNode.simulate");
		
		int index = (int)(Math.random()*3);
		
		if(rules.maxbet==0)
			index = (int)(Math.random()*2);
		

		
		if(rules.nextplayer(rules.actualplayer, index)==-1){
			//Controller.debugWindow.printMessage("ide nem kéne bekerülini:BotNode.simulate");
			children = new TreeNode[nCard];
		}
		else
			children = new TreeNode[nActions];
		
			if(rules.nextplayer(rules.actualplayer, index)==rules.myseat)
				children[index] = new BotNode();
			else
				if(rules.nextplayer(rules.actualplayer, index)==-1)
					children[index] = new CardNode();
				else
					children[index] = new OpponentNode();
		
		
		currentNode = currentNode.children[index];
		
		if(index==0)
			currentNode.creatNode(this,index, rules.getPlayerID(rules.nextplayer(rules.actualplayer, index)), 0, level);
		if(index==1)
			currentNode.creatNode(this,index, rules.getPlayerID(rules.nextplayer(rules.actualplayer, index)), rules.amunttocall(rules.actualplayer), level);
		if(index==2)
			if(rules.round<2)
				currentNode.creatNode(this,index, rules.getPlayerID(rules.nextplayer(rules.actualplayer, index)), rules.amunttocall(rules.actualplayer)+rules.fixlimit, level);
			else
				currentNode.creatNode(this,index, rules.getPlayerID(rules.nextplayer(rules.actualplayer, index)), rules.amunttocall(rules.actualplayer)+2*rules.fixlimit, level);


		return currentNode;
	}

	
	public TreeNode selectNoFold(TreeNode currentNode){
		
		//currentNode.typeOfNode= NodeType.playeraction_my;
		
		if (currentNode.isLeaf())
			currentNode.expand(currentNode);
		
		double children0tmp=1;
		double children1tmp=1;

		
		
		
		if (currentNode.children[1].nVisits == 0)
			currentNode = currentNode.children[1];
		else if (currentNode.children[2].nVisits == 0)
			currentNode = currentNode.children[2];
		else {
			children0tmp = currentNode.children[1].totValue
					+ c
					* Math.sqrt(Math.log10(nVisits)
							/ currentNode.children[1].nVisits);
			children1tmp = currentNode.children[2].totValue
					+ c
					* Math.sqrt(Math.log10(nVisits)
							/ currentNode.children[2].nVisits);
			
			double children0 = children0tmp
					/ (children0tmp + children1tmp);
			double children1 = children1tmp
					/ (children0tmp + children1tmp);
		
			
			double random = Math.random();
			if (random < children0)
				currentNode = currentNode.children[1];
			else 
				currentNode = currentNode.children[1];
		}
		
		String tmp3 ="NoFold selection: children0tmp=" + children0tmp + ", children1tmp=" + children1tmp;
		Controller.debugWindow.printMessage(tmp3);
		
		
		return currentNode;
		
	}
}
